package com.yixun.qfbao.hander;

import com.yixun.qfbao.api.service.BaseCodeValueService;
import com.yixun.qfbao.apidto.AddBaseCodeValueReqDto;
import com.yixun.qfbao.apidto.AddBaseCodeValueResDto;
import com.yixun.qfbao.dto.base.BaseRequestDTO;
import com.yixun.qfbao.dto.base.BaseResponseDTO;
import com.yixun.qfbao.hander.base.CommonAbstractHander;
import com.yixun.qfbao.model.PartnerInfo;
import com.yixun.qfbao.utils.LoginUtils;
import com.yixun.qfbao.vo.AddBaseCodeValueReqVo;
import com.yixun.qfbao.xf.model.enums.ResultEnum;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 类型值新增
 * @author guifang.weng
 * @create 2017-11-24 10:31
 **/
@Service
public class AddBaseCodeValueHander extends CommonAbstractHander {
    private static Logger logger= LoggerFactory.getLogger(AddBaseCodeValueHander.class);

    @Autowired
    private BaseCodeValueService baseCodeValueService;

    @Autowired
    private LoginUtils loginUtils;

    @Override
    public BaseResponseDTO doBusiness(BaseRequestDTO reqDto) {
        BaseResponseDTO<AddBaseCodeValueResDto> res = new BaseResponseDTO<>();
        AddBaseCodeValueReqVo reqVo = (AddBaseCodeValueReqVo)reqDto;
        AddBaseCodeValueReqDto addBaseCodeValueReqDto = reqVo.getParams();
        PartnerInfo pi = loginUtils.getUser(addBaseCodeValueReqDto.getUserId());
        if (pi == null) {
            res.setResult(ResultEnum.FAIL.getNo());
            res.setErrorMsg("未找到用户信息");
            return res;
        }
        addBaseCodeValueReqDto.setMerchantId(pi.getHigherid());
        try {
            AddBaseCodeValueResDto resDto = baseCodeValueService.addBaseCodeValue(addBaseCodeValueReqDto);
            res.setResult(resDto.getResult());
            res.setResData(resDto);
        } catch (Exception e) {
            logger.error("AddBaseCodeValueHander error: {}", e);
            res.setResult(ResultEnum.FAIL.getNo());
            res.setErrorMsg("新增类型值失败");
        }
        return res;
    }
}
